Page({
  data: {

    questions: [{
        title: "1. 中国的“文化遗产日”是哪一天？",
        options: ["A. 每年六月的第一个星期六", "B. 每年六月的第二个星期六", "C. 每年六月的第一个星期日", "D. 每年六月的第二个星期日"],
        answer: "C"
      },
      {
        title: "2. 三灶编织是什么级别的非物质文化遗产？",
        options: ["A. 国家级", "B. 省级", "C. 市级", "D. 区级"],
        answer: "A"
      },
      {
        title: "3. 三灶鹤舞是什么级别的非物质文化遗产？",
        options: ["A. 国家级", "B. 省级", "C. 市级", "D. 区级"],
        answer: "A"
      },
      {
        title: "4. 三灶鹤舞距今已有多少年历史？",
        options: ["A. 900年", "B. 700年", "C. 600年", "D. 500年"],
        answer: "B"
      },
      {
        title: "5. 三灶鹤舞被列入第几批国家级非物质文化遗产名录？",
        options: ["A. 第一批", "B. 第二批", "C. 第三批", "D. 第五批"],
        answer: "C"
      },
      {
        title: "6. 以下哪一种是三灶剪纸独创？",
        options: ["A. 花瓣纹", "B. 鱼鳞纹", "C. 月牙纹", "D. 梳子纹"],
        answer: "C"
      },
      {
        title: "7. 以下哪一个不是定家湾茶果的别称？",
        options: ["A. 状元糍", "B. 糯米糍", "C. 油糍", "D. 糍粑"],
        answer: "D"
      },
      {
        title: "8. 三灶编织技艺主要有哪几种？",
        options: ["A. 竹编", "B. 棕编", "C. 草编", "D. 藤编"],
        answer: "A"
      },
      {
        title: "9. 以下哪些是三灶鹤舞表演程序？",
        options: ["A. 开光", "B. 奏乐", "C. 拜老", "D. 羽化"],
        answer: "A"
      },
      {
        title: "10. 以下哪些是三灶镇海澄村的非遗项目？",
        options: ["A. 三灶编织", "B. 三灶鹤舞", "C. 三灶剪纸", "D. 定家湾茶果"],
        answer: "B"
      }
    ],
    // 其他题目...
    currentQuestionIndex: 0,
    currentQuestion: {},
    score: 0,
    completed: false,
    feedback: "",
    totalQuestions: 0
  },

  onLoad: function () {
    this.setData({
      totalQuestions: this.data.questions.length
    });
    this.setCurrentQuestion();
  },

  setCurrentQuestion: function () {
    this.setData({
      currentQuestion: this.data.questions[this.data.currentQuestionIndex]
    });
  },

  selectOption: function (e) {
    if (this.data.completed) return; // 如果已经答题完毕，则不再处理选择

    const selectedOptionIndex = e.currentTarget.dataset.index;
    const selectedOption = this.data.currentQuestion.options[selectedOptionIndex];
    const correctAnswer = this.data.currentQuestion.answer;

    if (selectedOption[0] === correctAnswer) {
      // 回答正确
      this.setData({
        score: this.data.score + 1,
        feedback: "回答正确！"
      });
    } else {
      // 回答错误
      this.setData({
        feedback: "回答错误！"
      });
    }

    if (this.data.currentQuestionIndex < this.data.questions.length - 1) {
      // 还有下一题
      this.setData({
        currentQuestionIndex: this.data.currentQuestionIndex + 1
      });
      this.setCurrentQuestion();
    } else {
      // 已经是最后一题
      this.setData({
        completed: true
      });
      wx.showToast({
        title: "答题完成！",
        icon: "none",
        duration: 2000,
        complete: () => {
          wx.showModal({
            title: "得分统计",
            content: `您的总分为 ${this.data.score} 分，共 ${this.data.totalQuestions} 题。是否返回主页面？`,
            success(res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/xuanuchuan/xuanchuan'
                });
              }
            }
          });
        }
      });
    }
  }
});